<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_INS_SHOP_RHIZOME  extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table = "M_INS_SHOP_RHIZOME";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "shop_cd";
	 
	 protected $_cols     = array(
		"ins_shop_cd"         => array(AOKI_VARTYPE_STR),
		"matrix_cd"       => array(AOKI_VARTYPE_INT),
		"now_quantity"    => array(AOKI_VARTYPE_INT),
		"last_quantity"   => array(AOKI_VARTYPE_INT),
		"diff_quantity"   => array(AOKI_VARTYPE_INT),
		"rate_quantity"   => array(AOKI_VARTYPE_INT),
	    "now_sales"       => array(AOKI_VARTYPE_INT),
		"last_sales"      => array(AOKI_VARTYPE_INT),
		"diff_sales"      => array(AOKI_VARTYPE_INT),
		"rate_sales"      => array(AOKI_VARTYPE_INT),
		"now_price"       => array(AOKI_VARTYPE_INT),
		"last_price"      => array(AOKI_VARTYPE_INT),
		"diff_price"      => array(AOKI_VARTYPE_INT),
		"rate_price"      => array(AOKI_VARTYPE_INT),
	 );
	
	 protected $_checks  = array(
		"ins_shop_cd"         => array( "EXIST","NUM" ),
		"now_quantity"    => array( "NUM" ),
		"last_quantity"   => array( "NUM" ),
		"diff_quantity"   => array( "NUM" ),
		"rate_quantity"   => array( "NUM" ),
		"now_sales"       => array( "NUM" ),
		"last_sales"      => array( "NUM" ),
		"diff_sales"      => array( "NUM" ),
		"rate_sales"      => array( "NUM" ),
		"now_price"       => array( "NUM" ),
		"last_price"      => array( "NUM" ),
		"diff_price"      => array( "NUM" ),
		"rate_price"      => array( "NUM" )
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		
	 );
	
	 public function search($where ,&$page = NULL ){
		$sql = "SELECT a.*,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name from {$this->_table}  a ";
		$sqlcnt = "SELECT COUNT(*) AS cnt FROM {$this->_table}  a ";
		return  parent::search($where ,$page ,$sql, $sqlcnt);
	}
	
	 
	 public function searchdmcsv($matrix_cd){
	 	$year4= substr($matrix_cd,0,4);
	 	$sql = "select  dm.*,ma.shop_cd,ma.count,
			ma.count * (CASE when ma.sort= 1 then mb.teigata 
		    when ma.sort= 2 then mb.vatutyaku 
		    when ma.sort= 3 then mb.zatutyaku
		    when ma.sort= 4 then mb.fuusyo
		    when ma.sort= 5 then mb.tp
		    else 0 end)  as money
			 from (select  a.shop_cd, a.count ,a.dm_no, dm.sort, 
		   (select  id from *M_COST_DM_PRICE* where a.count >= num_from and a.count <=num_to and year4 = {$year4}) as pid 
		  from M_COST_DM_COUNT a 
		  inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
		  where (dm.matrix_cd =  '{$matrix_cd}' ) )  ma
			inner join  *M_COST_DM_PRICE* mb on ma.pid = mb.id 
		inner join  M_COST_DM  dm on ma.dm_no = dm.dm_no 
			where dm.matrix_cd =  '{$matrix_cd}'  order by dm.dm_no,ma.shop_cd ";
			
		try{	
			$this->_db->query( $sql );
			
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return NULL;
		}
	 }
	 
	public function searchfliercsv($matrix_cd){
		$year4= substr($matrix_cd,0,4);
		$sql ="select  ma.flier_date, aa.shop_cd,aa.title,aa.size, aa.pnums, " .
				"if( right(cc.mark,1)='G',aa.pnums * cc.ins_load_ratio ,aa.pnums) * bb.price as now_cost,  " .
				"d.ins_shop_cd, d.ins_shop_name,d.media_name " .
				" from T_MON_CELLD_F aa " .
			 " inner join M_INS_SHOP_SHOP." .$matrix_cd.
			 " c on    aa.shop_cd = c.shop_cd" .
			 " inner join M_INS_SHOP  d   on c.ins_shop_cd = d.ins_shop_cd " .
			 " inner join M_COST_FLIER_PRICE bb on aa.shop_cd = bb.shop_cd and bb.year4={$year4} and aa.size = bb.size " .
			 " inner join M_MAP_GROUP_SHOP cc on aa.map_no = cc.map_no and aa.matrix_cd = cc.matrix_cd and aa.shop_cd = cc.shop_cd   " .		
			 " inner join M_MATRIX_VER m on m.matrix_cd = '{$matrix_cd}'  and m.id = aa.title_cd " .
			 " inner join M_MATRIX_TERM ma on  ma.matrix_cd =  '{$matrix_cd}'  " .
			 "  where aa.matrix_cd = '{$matrix_cd}' " .
			 " order by m.id,aa.shop_cd ";
			
			try{	
				$this->_db->query( $sql );
				
				$data = array();
				while( $row = $this->_db->fetch() ){
					$data[] = $row;
				}
				return $data;
			} catch (Exception $e) {
				echo "例外キャッチ：", $e->getMessage(), "\n";
				return NULL;
			}
	  }
	 
}

?>